home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP12.ZIP / PATRON / PAGES.CPP < prev    next >
C/C++ Source or Header  |  1993-06-27  |  30KB  |  1,363 lines

  1. /*
  2.  * PAGES.CPP
  3.  * Modifications for Chapter 12
  4.  *
  5.  * Implementation of the CPages class.  See PAGEWIN.CPP for additional
  6.  * member functions.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. HWND        g_hDlgPrint=NULL;
  23. BOOL        g_fCancelPrint=FALSE;
  24.  
  25.  
  26. /*
  27.  * CPages:CPages
  28.  * CPages::~CPages
  29.  *
  30.  * Constructor Parameters:
  31.  *  hInst           HINSTANCE of the application we're in.
  32.  *  cf              UINT application clipboard format.
  33.  */
  34.  
  35. CPages::CPages(HINSTANCE hInst, UINT cf)
  36.     : CWindow(hInst)
  37.     {
  38.     m_pPageCur=NULL;
  39.     m_iPageCur=0xFFFF;  //Pages are 0 indexed, so this is one before that.
  40.     m_cPages=0;
  41.     m_hWndPageList=NULL;
  42.  
  43.     /*
  44.      * Initialize to 2.54cm*2.54cm which is a page with no space for anything,
  45.      * just margins.  2.54cm=.5 inches on each margin.
  46.      */
  47.     m_cx=LOMETRIC_PER_INCH;
  48.     m_cy=LOMETRIC_PER_INCH;
  49.  
  50.     m_xPos=0L;
  51.     m_yPos=0L;
  52.  
  53.     m_dwIDNext=0;
  54.     m_pIStorage=NULL;
  55.  
  56.     m_fDirty=FALSE;
  57.     m_cf=cf;
  58.  
  59.     m_fDragSource=FALSE;
  60.     m_fMoveInPage=FALSE;
  61.     //CHAPTER12MOD
  62.     m_fLinkAllowed=FALSE;
  63.     //End CHAPTER12MOD
  64.  
  65.     m_fDragRectShown=FALSE;
  66.  
  67.     m_uScrollInset=GetProfileInt("windows", "DragScrollInset", DD_DEFSCROLLINSET);
  68.     m_uScrollDelay=GetProfileInt("windows", "DragScrollDelay", DD_DEFSCROLLDELAY);
  69.  
  70.     m_uHScrollCode=0;
  71.     m_uVScrollCode=0;
  72.  
  73.     //CHAPTER12MOD
  74.     m_fShowTypes=FALSE;
  75.     //End CHAPTER12MOD
  76.  
  77.     return;
  78.     }
  79.  
  80.  
  81. CPages::~CPages(void)
  82.     {
  83.     //Insure memory is cleaned up in list, and do final IStorage::Release
  84.     FIStorageSet(NULL, FALSE, FALSE);
  85.  
  86.     if (NULL!=m_hFont && !m_fSystemFont)
  87.         DeleteObject(m_hFont);
  88.  
  89.     if (NULL!=m_hWndPageList)
  90.         DestroyWindow(m_hWndPageList);
  91.  
  92.     //m_hWnd destroyed with the document.
  93.     return;
  94.     }
  95.  
  96.  
  97.  
  98. /*
  99.  * CPages::FIsDirty
  100.  *
  101.  * Purpose:
  102.  *  Tells the caller (document) if anything's happened to dirty us.
  103.  *
  104.  * Parameters:
  105.  *  None
  106.  *
  107.  * Return Value:
  108.  *  None
  109.  */
  110.  
  111. BOOL CPages::FIsDirty(void)
  112.     {
  113.     return m_fDirty;
  114.     }
  115.  
  116.  
  117.  
  118. /*
  119.  * CPages::FInit
  120.  *
  121.  * Purpose:
  122.  *  Instantiates a pages window within a given parent.  The
  123.  *  parent may be a main application window, could be an MDI child
  124.  *  window. We really do not care.
  125.  *
  126.  * Parameters:
  127.  *  hWndParent      HWND of the parent of this window
  128.  *  pRect           LPRECT that this window should occupy
  129.  *  dwStyle         DWORD containing the window's style flags.  Should
  130.  *                  contain WS_CHILD | WS_VISIBLE in typical circumstances.
  131.  *  uID             UINT ID to associate with this window
  132.  *  pv              LPVOID unused for now.
  133.  *
  134.  * Return Value:
  135.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  136.  */
  137.  
  138. BOOL CPages::FInit(HWND hWndParent, LPRECT pRect, DWORD dwStyle
  139.     , UINT uID, LPVOID pv)
  140.     {
  141.     int     cy;
  142.  
  143.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPAGES
  144.         , SZCLASSPAGES, dwStyle, pRect->left, pRect->top
  145.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  146.         , hWndParent, (HMENU)uID, m_hInst, (LPVOID)this);
  147.  
  148.     if (NULL==m_hWnd)
  149.         return FALSE;
  150.  
  151.     /*
  152.      * Create the hidden listbox we'll use to track pages.  We give it
  153.      * the owner-draw style so we can just store pointers in it.  We have
  154.      * to set the parent to NULL such that the window hangs around after
  155.      * the pages window is destroyed so that we can clean up the
  156.      * memory stored in it from the CPages destructor.
  157.      */
  158.     m_hWndPageList=CreateWindow("listbox", "Page List", WS_POPUP | LBS_OWNERDRAWFIXED
  159.         , 0, 0, 100, 100, HWND_DESKTOP, NULL, m_hInst, NULL);
  160.  
  161.     if (NULL==m_hWndPageList)
  162.         return FALSE;
  163.  
  164.     //Create a 14 point Arial font, or use the system variable font.
  165.     cy=MulDiv(-14, LOMETRIC_PER_INCH, 72);
  166.     m_hFont=CreateFont(cy, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE
  167.         , ANSI_CHARSET, OUT_TT_PRECIS, CLIP_TT_ALWAYS, PROOF_QUALITY
  168.         , VARIABLE_PITCH | FF_SWISS, "Arial");
  169.  
  170.     if (NULL==m_hFont)
  171.         {
  172.         m_hFont=(HFONT)GetStockObject(ANSI_VAR_FONT);
  173.         m_fSystemFont=TRUE;
  174.         }
  175.  
  176.     return TRUE;
  177.     }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. /*
  185.  * CPages::FIStorageSet
  186.  *
  187.  * Purpose:
  188.  *  Provides the document's IStorage to the pages for its own uses.
  189.  *  If this is a new storage, then we initalize it with streams we
  190.  *  want to always exists.  If this is an open, then we create
  191.  *  our page list from the PageList string we wrote before.
  192.  *
  193.  * Parameters:
  194.  *  pIStorage       LPSTORAGE to the new or opened storage.  If this is
  195.  *                  NULL then we just clean up and exit.
  196.  *  fChange         BOOL indicating is this was a Save As operation
  197.  *                  meaning that we have the structure already, we
  198.  *                  just need to change our value of m_pIStorage.
  199.  *  fInitNew        BOOL indicating if this is a new storage or one
  200.  *                  opened from a previous save.
  201.  *
  202.  * Return Value:
  203.  *  None
  204.  */
  205.  
  206. BOOL CPages::FIStorageSet(LPSTORAGE pIStorage, BOOL fChange, BOOL fInitNew)
  207.     {
  208.     DWORD           dwMode=STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  209.     HRESULT         hr;
  210.     LPPAGE          pPage;
  211.     BOOL            fRet=FALSE;
  212.     ULONG           cbRead;
  213.     PAGELIST        pgList;
  214.     LPSTREAM        pIStream;
  215.     LPMALLOC        pIMalloc;
  216.     LPDWORD         pdwID;
  217.     UINT            i;
  218.  
  219.     //If we're just changing saved roots, just open the current page again
  220.     if (fChange)
  221.         {
  222.         if (NULL==pIStorage)
  223.             return FALSE;
  224.  
  225.         m_pIStorage->Release();
  226.         m_pIStorage=pIStorage;
  227.         m_pIStorage->AddRef();
  228.  
  229.         FPageGet(m_iPageCur, &m_pPageCur, TRUE);
  230.         return TRUE;
  231.         }
  232.  
  233.     if (NULL!=m_hWndPageList)
  234.         {
  235.         //On new or open, clean out whatever it is we have.
  236.         for (i=0; i < m_cPages; i++)
  237.             {
  238.             if (FPageGet(i, &pPage, FALSE))
  239.                 delete pPage;
  240.             }
  241.  
  242.         SendMessage(m_hWndPageList, LB_RESETCONTENT, 0, 0L);
  243.         }
  244.  
  245.     if (NULL!=m_pIStorage)
  246.         m_pIStorage->Release();
  247.  
  248.     m_pIStorage=NULL;
  249.  
  250.     //If we're just cleaning up, then we're done.
  251.     if (NULL==pIStorage)
  252.         return TRUE;
  253.  
  254.     m_pIStorage=pIStorage;
  255.     m_pIStorage->AddRef();
  256.  
  257.     //If this is a new storage, create the streams we require
  258.     if (fInitNew)
  259.         {
  260.         //Page list header.
  261.         hr=m_pIStorage->CreateStream(SZSTREAMPAGELIST, dwMode | STGM_CREATE
  262.             , 0, 0, &pIStream);
  263.  
  264.         if (FAILED(hr))
  265.             return FALSE;
  266.  
  267.         pIStream->Release();
  268.  
  269.         //Device Configuration
  270.         hr=m_pIStorage->CreateStream(SZSTREAMDEVICECONFIG, dwMode | STGM_CREATE
  271.             , 0, 0, &pIStream);
  272.  
  273.         if (FAILED(hr))
  274.             return FALSE;
  275.  
  276.         pIStream->Release();
  277.         return TRUE;
  278.         }
  279.  
  280.  
  281.     /*
  282.      * We're opening an existing file:
  283.      *  1)  Configure for the device we're on
  284.      *  2)  Read the Page List and create page entries for each.
  285.      */
  286.  
  287.     ConfigureForDevice();
  288.  
  289.     //Read the page list.
  290.     hr=m_pIStorage->OpenStream(SZSTREAMPAGELIST, NULL, dwMode, 0, &pIStream);
  291.  
  292.     if (FAILED(hr))
  293.         return FALSE;
  294.  
  295.     if (SUCCEEDED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
  296.         {
  297.         pIStream->Read((LPVOID)&pgList, sizeof(PAGELIST), &cbRead);
  298.         m_cPages  =(UINT)pgList.cPages;
  299.         m_iPageCur=(UINT)pgList.iPageCur;
  300.         m_dwIDNext=pgList.dwIDNext;
  301.  
  302.         fRet=TRUE;
  303.         cbRead=pgList.cPages*sizeof(DWORD);
  304.  
  305.         if (0!=cbRead)
  306.             {
  307.             pdwID=(LPDWORD)pIMalloc->Alloc(cbRead);
  308.  
  309.             if (NULL!=pdwID)
  310.                 {
  311.                 pIStream->Read((LPVOID)pdwID, cbRead, &cbRead);
  312.  
  313.                 for (i=0; i < m_cPages; i++)
  314.                     fRet &=F